﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the API Concepts Framework
 * http://apiconceptsframework.codeplex.com
 *
 * Copyright (C) 2014 eva Kühn, Thomas Scheller, Vienna University of Technology
 * http://www.complang.tuwien.ac.at/eva
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using ApiConceptsFramework.Contract;
using System.Linq;

namespace ApiConceptsFramework.Concepts.Properties
{
	public class NumberOfOverloads : IMeasurableProperty
	{
		internal const float NumberOfOverloadsToComplexityFactor = 1.0f;//0.3f;
		
		public float CalculateComplexity(MeasurableTarget target, IDE ide = IDE.VisualStudioOrSimilar)
		{
			//TODO different handling for visual studio and eclipse??

			if (!(target.Member is MethodInfo) && !(target.Member is ConstructorInfo))
				throw new ArgumentException("Target must be a MethodInfo or ConstructorInfo!", "target");
			
			IEnumerable<MethodBase> overloads;
			if (target.Member is MethodInfo)
				overloads = GetMethodOverloads(target);
			else //if (member is ConstructorInfo)
				overloads = GetConstructorOverloads(target);

			//var firstOverload = overloads.First();
			//if (firstOverload.Equals(target.Member))
			//	return 0; //no complexity when the member is the first overload
			//return (overloads.Count() - 1) * NumberOfOverloadsToComplexityFactor;
			var overloadsBefore = CountMembersBefore(overloads, target.Member, target.Type.Name);
			CalculationInfo = overloadsBefore + " overloads before this one";
			return overloadsBefore * NumberOfOverloadsToComplexityFactor;
		}

		private static IEnumerable<MethodInfo> GetMethodOverloads(MeasurableTarget target)
		{
			return target.GetMembersForComplexityCalculation(target.Member.IsStatic())
						 .Where(m => (m is MethodInfo) && m.Name == target.Member.Name)
			             .Select(m => (MethodInfo) m)
			             .OrderBy(m => m.GetParameters().Length)
			             .ThenBy(m => m.ToPrintableString(target.Type.Name)) //order first by number of params, then alphabetically
			             .ToList();
			//NOTE: for Ordering, ToPrintableString is used instead of the normal ToString method, because the ordering needs to be done
			//without the namespace names of the parameter types (e.g. not System.String but String)
		}

		private static IEnumerable<ConstructorInfo> GetConstructorOverloads(MeasurableTarget target)
		{
			return target.Type.GetConstructors()
						 .OrderBy(m => m.GetParameters().Length)
						 .ThenBy(m => m.ToPrintableString(target.Type.Name)) //order first by number of params, then alphabetically
						 .ToList();
		}

		public IEnumerable<string> GetSuggestions(MeasurableTarget target, IDE ide = IDE.VisualStudioOrSimilar)
		{
			return new string[] {}; //currently no suggestions
		}

		public string CalculationInfo { get; private set; }

		public int CountMembersBefore(IEnumerable<MethodBase> members, MemberInfo member, string typename)
		{
			//string memberName = member.ToPrintableString(typename);
			return members
				//.Select(m => new {Member = m, Name = m.ToPrintableString(typename)})
				//.OrderBy(m => m.Member.GetParameters().Count())
				//.ThenBy(m => m.Name)
				.TakeWhile(m => m != member).Count();
		}
	}
}